package operation;

import book.Book;
import book.BookLIst;

import java.util.NoSuchElementException;
import java.util.Scanner;

public class DelBook implements Ioperation{
    public void work(BookLIst books){
        System.out.println("删除图书");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要删除的书籍名称：");
        String bookName = sc.nextLine();
        //查看书籍是否存在
        int index = -1;
        int currentSize = books.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = books.getBook(i);
            if(book.getName().equals(bookName)){
                index = i;
                break;
            }
        }
        if(index == -1){
            System.out.println("没有找到你要删除的书籍");
            return;
        }
        //找到了的话开始删除
        //将要删除的book移动到数组最后一位 防止删除后出现数组空缺
        for(int j = index; j < currentSize - 1; j++){
            books.setBookList(j,books.getBook(j + 1));
        }
        books.setUsedSize(currentSize - 1);
        books.setBookList(currentSize - 1, null);
        System.out.println("删除成功");
    }
}
